﻿Namespace Models

    Public Interface IDepartamentos
        Function BuscarDepartamento(parametro As String) As List(Of DEPARTAMENTO_DATATYPE)

        Function BuscarMunicipio(Departamento As Integer, parametro As String) As List(Of MUNICIPIO_DATATYPE)

        Function BuscarPais(parametro As String) As List(Of PAIS_DATATYPE)

        Function BuscarCentroEscolar(parametro As String) As List(Of CENTRO_EDUCATIVO_DATATYPE)

        Function GetAllArraryMunicipios() As IEnumerable(Of Object)

        Function GetAllPaises() As IEnumerable(Of Object)

        Function GetAllCentrosEscolares() As IEnumerable(Of Object)
    End Interface


    Partial Public Class SchoolClickDB_Repositorio
        Implements IDepartamentos

        Public ReadOnly Property IDepartamentos() As IDepartamentos
            Get
                Return DirectCast(Me, IDepartamentos)
            End Get
        End Property

        Private Function BuscarDepartamento(parametro As String) As List(Of DEPARTAMENTO_DATATYPE) Implements IDepartamentos.BuscarDepartamento
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.FIND_DEPARTAMENTO(parametro).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido realizar la busqueda de departamentos en el sistema. {0}", ex.Message), ex)
            End Try
        End Function

        Private Function BuscarMunicipio(Departamento As Integer, parametro As String) As List(Of MUNICIPIO_DATATYPE) Implements IDepartamentos.BuscarMunicipio
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.FIND_MUNICIPIO(Departamento, parametro).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido realizar la busqueda de municipios en el sistema. {0}", ex.Message), ex)
            End Try
        End Function

        Private Function BuscarPais(parametro As String) As List(Of PAIS_DATATYPE) Implements IDepartamentos.BuscarPais
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.FIND_PAIS(parametro).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido realizar la busqueda de paises en el sistema. {0}", ex.Message), ex)
            End Try
        End Function

        Private Function BuscarCentroEscolar(parametro As String) As List(Of CENTRO_EDUCATIVO_DATATYPE) Implements IDepartamentos.BuscarCentroEscolar
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.FIND_CEPROCEDENCIA(parametro).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido realizar la busqueda de los Centros Educativos del pais en el sistema. {0}", ex.Message), ex)
            End Try
        End Function

        Private Function GetAllArraryMunicipios() As IEnumerable(Of Object) Implements IDepartamentos.GetAllArraryMunicipios
            Try
                Using cnn As New SchoolClickEntities
                    Dim _arrDepto = cnn.MUNICIPIOS.Select(Function(m) m.ID_DEPARTAMENTO).Distinct().ToArray
                    Dim _lstMunicios As New List(Of Object)

                    'Insertamos el registro por defecto cuando no se haya seleccionado ningun departamento
                    Dim _arrDataDef As New List(Of Object)
                    _arrDataDef.Add(New With {.label = "Departamento NO especificado", .value = 0})
                    _lstMunicios.Add(_arrDataDef)

                    'Armamos todo el array de los departamentos con sus municipios
                    For Each dpto In _arrDepto
                        Dim arrData As New List(Of Object)
                        For Each mun As MUNICIPIO In cnn.MUNICIPIOS.Where(Function(m) m.ID_DEPARTAMENTO = dpto)
                            arrData.Add(New With {.label = mun.NOMBRE_MUNICIPIO})
                        Next
                        _lstMunicios.Add(arrData)
                    Next
                    Return _lstMunicios
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido recuperar los municipios de El Salvador en el sistema. {0}", ex.Message), ex)
            End Try
        End Function

        Private Function GetAllPaises() As IEnumerable(Of Object) Implements IDepartamentos.GetAllPaises
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.PAISES.Select(Function(p) New With {.label = p.PAI_NOMBRE}).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido recuperar los paises disponibles en el sistema. {0}", ex.Message), ex)
            End Try
        End Function

        Private Function GetAllCentrosEscolares() As IEnumerable(Of Object) Implements IDepartamentos.GetAllCentrosEscolares
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.CENTROS_EDUCATIVOS.Select(Function(p) New With {.label = p.NOMBRE_CEDUCATIVO}).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se ha podido recuperar los Centros Educativos disponibles en el sistema. {0}", ex.Message), ex)
            End Try
        End Function
    End Class
End Namespace